package com.mpp.admin.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mapp
 * @des
 * @date 2019/3/14
 */
@Slf4j
public class TemplateUtil {

    private static Configuration config = null;

    public static Template getTemplate(String template) {
        Template temp = null;
        try {
            if (config == null) {
                config = new Configuration(Configuration.VERSION_2_3_23);
                config.setDirectoryForTemplateLoading(new File("src/main/resources/generate"));
                config.setDefaultEncoding("UTF-8");
            }
            temp = config.getTemplate(template);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("文件夹不存在");
        }
        return temp;
    }

    public static void main(String[] args) throws IOException, TemplateException {
        Template template = getTemplate("domain.ftl");
        Map<String, String> map = new HashMap<>();
        map.put("name", "哈哈");
        Writer outputStream = new FileWriter("D:\\a.txt");
        template.process(map, outputStream);
    }
}
